const mMgr = wx.getBackgroundAudioManager()

Component({

    properties: {
        img: String,
        content: String,
        url: String
    },

    data: {
        playing: false,
        playImg: "./images/player.png",
        stopImg: "./images/pause.png"
    },

    attached() {
        this._recoverStatus()
        this._monitorSwitch()
    },


    methods: {
        /**
         * 音乐的播放暂停控制
         * 播放->暂停/ 暂停->播放
         */
        onPlay() {
            let playing = this.data.playing
            this.setData({
                playing: !playing
            })
            if (playing) {
                // 播放状态
                mMgr.pause()
                return
            }

            // 未播放状态
            mMgr.src = this.properties.url
            mMgr.title = '佛系title'
        },

        //恢复播放状态
        _recoverStatus() {
            if (mMgr.paused) {
                this.setData({
                    playing: false
                })
                return
            }
            if (mMgr.src == this.properties.url) {
                this.setData({
                    playing: true
                })
            }
        },

        //监听背景音乐控制面板
        _monitorSwitch() {
            mMgr.onPlay(() => {
                this._recoverStatus()
            })
            mMgr.onPause(() => {
                this._recoverStatus()
            })
            mMgr.onStop(() => {
                this._recoverStatus()
            })
            mMgr.onEnded(() => {
                this._recoverStatus()
            })

        }
    }
})